package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.service.EmployeeService;
import com.itheima.reggie.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Service
@Slf4j
public class EmployeeServiceImpl implements EmployeeService {
    @Resource
    private EmployeeMapper employeeMapper;

    @Override
    public Result<Employee> login(HttpServletRequest request, String username, String password) {
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername, username);
        Employee employee = employeeMapper.selectOne(queryWrapper);
        if (employee == null) {
            return Result.error("该用户名不存在!");
        }
        if (!employee.getPassword().equals(password)) {
            return Result.error("密码错误,登录失败!");
        }
        if (employee.getStatus() == 0) {
            return Result.error("账号已被禁用!");
        }
        request.getSession().setAttribute("employeeId", employee.getId());
        return Result.success(employee);
    }

    @Override
    public Result<String> logout(HttpServletRequest request) {
        request.getSession().removeAttribute("employeeId");
        return Result.success("退出成功");
    }

    @Override
    public Result<String> addOne(HttpServletRequest request, Employee employee) {
        String password = DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(password);
        employeeMapper.insert(employee);
        log.info("开始向数据库插入数据==>" + employee);
        return Result.success("添加成功");
    }

    @Override
    public Result<Page<Employee>> getPage(String name, int currentPage, int pageSize) {
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name), Employee::getName, name);
        queryWrapper.orderByDesc(Employee::getUpdateTime);
        Page<Employee> page = new Page<>(currentPage, pageSize);
        employeeMapper.selectPage(page, queryWrapper);
        log.info("从数据库查询出的页面==>" + page);
        return Result.success(page);
    }

    @Override
    public Result<String> update(HttpServletRequest request, Employee employee) {
       log.info("开始更新数据库中的数据");
        employeeMapper.updateById(employee);
        return Result.success("修改成功");
    }

    @Override
    public Result<Employee> getById(Long id) {
        log.info("开始根据id{}从数据库查询数据",id);
        Employee employee = employeeMapper.selectById(id);
        log.info("完成根据id{}从数据库查询数据,数据为{}",id,employee);
        return Result.success(employee);
    }
}
